/**
 * 
 */
package gameEngine;

import java.util.Observable;

/**
 * @author Joe R. Nassimian
 * @author Yannis Mazzer
 */
public class Board extends Observable{

	private int board[];
	private int xlength; // Horizontal coordinates starting from left to right;
	private int ylength; // Vertical coordinates starting from top to bottom;
	private int xEffLength; //
	private int yEffLength;
	private String boardShape;
	private int edgeSize;
	
	public Board(int _edgeSize, String _shape) {
		this.edgeSize = _edgeSize;
		this.setBoardShape(_shape);
		board = new int[edgeSize*edgeSize];
	}
	
	public void updateView(){
		setChanged();
		notifyObservers("init");
	}
	
	/**
	 * @param edgeSize the edgeSize to set
	 */
	public void setEdgeSize(int edgeSize) {
		this.edgeSize = edgeSize;
		hasChanged();
		notifyObservers();
	}

	public int[] get() {
		return board;
	}

	/**
	 * @param xlength the xlength to set
	 */
	public void setXLength(int _xlength) {
		this.xlength = _xlength;
		hasChanged();
		notifyObservers();
	}

	/**
	 * @return the xlength
	 */
	public int getXLength() {
		return xlength;
	}

	/**
	 * @param ylength the ylength to set
	 */
	public void setYLength(int _ylength) {
		this.ylength = _ylength;
		hasChanged();
		notifyObservers();
	}

	/**
	 * @return the ylength
	 */
	public int getYLength() {
		return ylength;
	}
	
	/**
	 * @param xEffLength the xEffLength to set
	 */
	public void setXEffLength(int _xEffLength) {
		this.xEffLength = _xEffLength;
		hasChanged();
		notifyObservers();
	}

	/**
	 * @return the xEffLength
	 */
	public int getXEffLength() {
		return xEffLength;
	}

	/**
	 * @param yEffLength the yEffLength to set
	 */
	public void setYEffLength(int _yEffLength) {
		this.yEffLength = _yEffLength;
		hasChanged();
		notifyObservers();
	}

	/**
	 * @return the yEffLength
	 */
	public int getYEffLength() {
		return yEffLength;
	}

	/**
	 * @return the length
	 */
	public int getEdgeSize() {
		return edgeSize;
	}	

	/**
	 * @param boardShape the boardShape to set
	 */
	public void setBoardShape(String boardShape) {
		this.boardShape = boardShape;
		hasChanged();
		notifyObservers();
	}

	/**
	 * @return the boardShape
	 */
	public String getBoardShape() {
		return boardShape;
	}
	
	public boolean isCaseFree(int iter) {
		if(board[iter] == 0) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean isCaseFree(int x, int y) {
		return isCaseFree(coordToIter(x,y));
	}
	
	public void putPiece(int iter, Piece piece) {
		int playerId = piece.get親().getId();
		for(int i = 0; i < piece.getConfiguration().length; i++) {
			for(int j = 0; j < piece.getConfiguration()[i].length; j++) {
				setCase(iter+i*edgeSize+piece.getConfiguration()[i][j], playerId);
			}
		}
		hasChanged();
		notifyObservers();
	}
	
	public void putPiece(int x, int y, Piece piece) {
		putPiece(coordToIter(x,y), piece);
	}
	
	public void setCase(int iter, int playerId) {
		board[iter] = playerId;
	}
	
	public void setCase(int x, int y, int playerId) {
		setCase(coordToIter(x,y), playerId);
	}
	
	public int coordToIter(int x, int y) {
		return ((y-1) * edgeSize + x);
	}
	
	public int[] iterToCoord(int i) {
		int[] res = {};
		res[0] = i % edgeSize;
		res[1] = i / edgeSize;
		return res;
	}

}
